package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallMechanismGoodsCategoryMapper;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallCategoryExample;
import org.linlinjava.litemall.db.domain.LitemallMechanismGoodsCategory;
import org.linlinjava.litemall.db.domain.LitemallMechanismGoodsCategoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhy
 * @create 2018-12-17 14:30
 **/
@Service
public class LitemallMechanismGoodsCategoryService {
	@Resource
	private LitemallMechanismGoodsCategoryMapper litemallMechanismGoodsCategoryMapper;

	public List<LitemallMechanismGoodsCategory> querySelective(String id, String name, Integer orgId, Integer page, Integer size, String sort, String order) {
		LitemallMechanismGoodsCategoryExample example = new LitemallMechanismGoodsCategoryExample();
		LitemallMechanismGoodsCategoryExample.Criteria criteria = example.createCriteria();

		if(!StringUtils.isEmpty(id)){
			criteria.andIdEqualTo(Integer.valueOf(id));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.andNameLike("%" + name + "%");
		}
		if(!StringUtils.isEmpty(orgId)){
			criteria.andMerchanismIdEqualTo(orgId);
		}
		criteria.andDeletedEqualTo(false);

		PageHelper.startPage(page, size);
		return litemallMechanismGoodsCategoryMapper.selectByExample(example);
	}

	public int countSelective(String id, String name, Integer orgId, Integer page, Integer size, String sort, String order) {
		LitemallMechanismGoodsCategoryExample example = new LitemallMechanismGoodsCategoryExample();
		LitemallMechanismGoodsCategoryExample.Criteria criteria = example.createCriteria();

		if(!StringUtils.isEmpty(id)){
			criteria.andIdEqualTo(Integer.valueOf(id));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.andNameLike("%" + name + "%");
		}
		if(!StringUtils.isEmpty(orgId)){
			criteria.andMerchanismIdEqualTo(orgId);
		}
		criteria.andDeletedEqualTo(false);

		return (int)litemallMechanismGoodsCategoryMapper.countByExample(example);
	}

	public void add(LitemallMechanismGoodsCategory category) {
		 litemallMechanismGoodsCategoryMapper.insertSelective(category);
	}

	public void updateById(LitemallMechanismGoodsCategory category) {
		litemallMechanismGoodsCategoryMapper.updateByPrimaryKeySelective(category);
	}

	public void deleteById(Integer id) {
		litemallMechanismGoodsCategoryMapper.logicalDeleteByPrimaryKey(id);
	}

	public List<LitemallMechanismGoodsCategory> queryByMerchanismId(Integer mechanismId) {
		LitemallMechanismGoodsCategoryExample example = new LitemallMechanismGoodsCategoryExample();
		example.or().andMerchanismIdEqualTo(mechanismId).andDeletedEqualTo(false);
		return litemallMechanismGoodsCategoryMapper.selectByExample(example);
	}

	public LitemallMechanismGoodsCategory findById(Integer id) {
		return litemallMechanismGoodsCategoryMapper.selectByPrimaryKey(id);
	}

	public List<LitemallMechanismGoodsCategory> queryByMechId(List<Integer> Ids){
		LitemallMechanismGoodsCategoryExample example = new LitemallMechanismGoodsCategoryExample();
		example.or().andMerchanismIdIn(Ids);
		return litemallMechanismGoodsCategoryMapper.selectByExample(example);
	}
}
